package Day2025_02_20;

public class LeetCode904 {
    public int totalFruit(int[] fruits) {
        int n = fruits.length;
        int maxLen = 0, kinds = 0;
        int[] hash = new int[n];
        for (int left = 0, right = 0; right < n; right++) {
            int in = fruits[right];
            if (hash[in]++ == 0) kinds++;
            while (kinds > 2) {
                int out = fruits[left++];
                if (hash[out]-- == 1) kinds--;
            }
            maxLen = Math.max(maxLen, right - left + 1);
        }
        return maxLen;
    }
}
